<?php
/**
 * generates a json of all thumb files
 * 
 * @author Sean
 * @return echos out json of all thumb files
 */
 
include ("connect.php");
include ("config.php");

$GoogleID = $_GET['googleId'];
$GoogleID = mysql_real_escape_string($GoogleID);

$SessionID = $_GET['sessionId'];
$SessionID = mysql_real_escape_string($SessionID);

$result = mysql_query("SELECT sessionID FROM users WHERE GoogleID = '" . $GoogleID . "'");
while ($row = mysql_fetch_array($result)) {
   $DB_SessionID = $row["sessionID"];
}

if($SessionID == $DB_SessionID){
	
	$pathToFiles = $pathToUsersFiles.$GoogleID."/thumbs/";

	// open the directory
	if(is_dir($pathToFiles)){

		if($dir = @opendir( $pathToFiles ) ){
			$array = array();

			// loop through it, looking for any/all JPG files:
			while (false !== ($fname = readdir( $dir ))) {
				// parse path for the extension
				$info = pathinfo($fname);
				
				if ($fname != "." && $fname != "thumbs" && $fname != "..")
				{
					if(strtolower($info['extension']) == "jpg" || strtolower($info['extension']) == "jpeg" || strtolower($info['extension']) == "png" ){
						$array[] = array("imageName"=>$GoogleID."/thumbs/".$fname);

					}			
					// header('Access-Control-Allow-Origin: *');
					 // echo $GoogleID.'/'.$fname."<br>";
				}
			}	
			
			$json = str_replace('\/', '/', json_encode($array));

			header('Access-Control-Allow-Origin: *');
			echo $json;
			
			closedir($dir);
		}
	}
	else{
		header('Access-Control-Allow-Origin: *');
		echo "ERROR: Directory does not exist.";
	}

}
else{
	header('Access-Control-Allow-Origin: *');
	header('Location: http://sean.servern.org/webdrop/Webdrop.html?#page=Login');
}
?>